package util;

import java.util.Iterator;
import java.util.*;

public abstract class CollezioneAstratta<T extends Comparable<? super T>>
		implements CollezioneOrdinata<T> {

	public int size() {
		int c = 0;
		for (T x : this)
			c++;

		return c;
	}

	public void clear() {
		Iterator<T> it = this.iterator();
		while (it.hasNext()) {
			it.next();
			it.remove();
		}
	}

	public void remove(T x) {
		Iterator<T> it = this.iterator();
		while (it.hasNext()) {
			it.next();
			it.remove();
			T y = it.next();
			if (y.equals(x)) {
				it.remove();
				return;
			}
			if (y.compareTo(x) > 0)
				return;
		}
	}

	public boolean contains(T x) {
		Iterator<T> it = this.iterator();
		while (it.hasNext()) {
			it.next();
			it.remove();
			T y = it.next();
			if (y.equals(x)) {

				return true;
			}
			if (y.compareTo(x) > 0)
				return false;
		}
		return false;

	}

	public T get(T x) {
		Iterator<T> it = this.iterator();
		while (it.hasNext()) {
			it.next();
			it.remove();
			T y = it.next();
			if (y.equals(x)) {

				return y;
			}
			if (y.compareTo(x) > 0)
				return null;
		}
		return null;

	}

	public boolean isEmpty() {
		return size() == 0;
	}

	public boolean isFull() {
		return false;// gli eredi devono fornire un implementazione.
	}

	public String toString() {
		StringBuilder sb = new StringBuilder(500);
		sb.append('[');
		Iterator<T> it = iterator();
		while (it.hasNext()) {
			sb.append(it.next());
			if (it.hasNext())
				sb.append(",b");
		}
		return sb.toString();
	}
	//fare equals hashcode 

}
